package com.carnation.utils;

import java.io.File;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

public class MenuFileUtril {
	private Logger logger = Logger.getLogger(MenuFileUtril.class);
	private static Properties p = new Properties();
	private final static String RESOURCENAME = "configure.properties";
	public final static String FILENAME = "filename";
	public final static String FILEINFODB = "fileinfo";
	public final static String USERINFODB = "userinfo";
	public final static String STOREROOT = "rootpath";
	public final static String LIMITSIZE= "limitsize";
	public final static String LIMITTYPE="limitfiletype";
	public final static String FTPHOST="ftp.host";
	public final static String FTPPORT="ftp.port";
	public final static String FTPUSER="ftp.user";
	public final static String FTPPASSWD="ftp.passwd";
	public final static String FTPPROTOCOL = "ftp.protocol";
	public final static String FTPSTORE = "ftp.store";
	public final static String SSHPORT = "ftp.ssh.port";
	public final static String FTPUPLOAD = "ftp.upload";
	
	public MenuFileUtril(){
		Init();
	}
	
	private static void Init(){
		try{
			InputStream is = MenuFileUtril.class.getClassLoader().getResourceAsStream(RESOURCENAME);
			p.load(is);
		}
		catch(Exception e){
			System.out.println("加载配置文件失败");
			e.printStackTrace();
			System.exit(0);
		}
	}
	
	public String getFileName(){
		return p.getProperty(FILENAME);
	}
	
	public String getFileNameWithPath(){
		String path = MenuFileUtril.class.getClassLoader().getResource("").getPath();
		return path + p.getProperty(FILENAME);
	}
	
	public String getFileInfoDBName(){
		return p.getProperty(FILEINFODB);
	}
	
	public String getFileInfoDBNameWithPath(){
		String path = MenuFileUtril.class.getClassLoader().getResource("").getPath();
		return path + p.getProperty(FILEINFODB);
	}
	
	public String getUserInfoDBName(){
		return p.getProperty(USERINFODB);
	}
	
	public String getUserInfoDBNameWithPath(){
		String path = MenuFileUtril.class.getClassLoader().getResource("").getPath();
		return path + p.getProperty(USERINFODB);
	}
	
	public String getRootPath(){
		String rootpath = p.getProperty(STOREROOT);
		File file = new File(rootpath);
		if(file.exists()&&file.isDirectory()){
			return rootpath;
		}
		else{
			logger.error("路径配置出错");
			System.exit(0);
			return null;
		}
	}
	
	public Long getLimitSize(){
		String size = p.getProperty(LIMITSIZE);
		try{
			Long s = Long.parseLong(size);
			return s * 1024 *1024;
		}
		catch(Exception e){
			logger.error("上传文件大小设置错误",e);
			System.exit(0);
			return null;
		}
	}
	public Long getLimitSizeM(){
		String size = p.getProperty(LIMITSIZE);
		try{
			Long s = Long.parseLong(size);
			return s ;
		}
		catch(Exception e){
			logger.error("上传文件大小设置错误",e);
			System.exit(0);
			return null;
		}
	}
	
	public String getFileType(){
		return p.getProperty(LIMITTYPE);
	}
	
	public String getFTPProtocol(){
		return p.getProperty(FTPPROTOCOL);
	}
	
	public String getFTPHost(){
		return p .getProperty(FTPHOST);
	}
	
	public int getFTPPort(){
		String portString = p.getProperty(FTPPORT);
		try{
			return Integer.valueOf(portString);
		}
		catch(Exception e){
			System.out.println("非法FTP端口");
			System.exit(0);
			return -1;
		}
	}
	
	public String getFTPUser(){
		return p.getProperty(FTPUSER);
	}
	
	public String getFTPPasswd(){
		return p.getProperty(FTPPASSWD);
	}
	
	public String getFTPStorePath(){
		return p.getProperty(FTPSTORE);
	}
	
	public int getSSHPort(){
		String sshport = p.getProperty(SSHPORT);
		try{
			return Integer.valueOf(sshport);
		}
		catch(Exception e){
			logger.error("配置文件中SSH端口错误",e);
			return 22;
		}
	}
	
	public String getUploadCronTime(){
		return p.getProperty(FTPUPLOAD);
	}
}
